/*
 * Copyright by LunaSec (owned by Refinery Labs, Inc)
 *
 * Licensed under the Business Source License v1.1
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * https://github.com/lunasec-io/lunasec/blob/master/licenses/BSL-LunaTrace.txt
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
export class Graph {
  private childIds: Map<string, string> = new Map();
  private parentIds: Map<string, Set<string>> = new Map();
  private nodeEdgeIds: Map<string, Set<string>> = new Map();

  public insertEdge(edgeId: string, parentId: string, childId: string) {
    this.childIds.set(parentId, childId);

    const existingParentEdges = this.parentIds.get(childId);
    if (!existingParentEdges) {
      this.parentIds.set(childId, new Set([parentId]));
    } else {
      existingParentEdges.add(parentId);
    }

    const existingEdgesToChild = this.nodeEdgeIds.get(childId);
    if (!existingEdgesToChild) {
      this.nodeEdgeIds.set(childId, new Set([edgeId]));
    } else {
      existingEdgesToChild.add(edgeId);
    }
  }

  public getParentIds(childId: string) {
    return this.parentIds.get(childId) || new Set();
  }

  public getNodeEdgeIds(childId: string) {
    return this.nodeEdgeIds.get(childId) || new Set();
  }
}
